const fs = require('fs')
const path = require('path')
const config = require('./config')
const App = require('./application')
const router = require('./router')
const { readFile } = require('./util')

// read my config
let myConfigPath = path.resolve(__dirname, './myConfig.json')
let myConfig = {}
if (fs.existsSync(myConfigPath)) {
    myConfig = Object.assign(myConfig, readFile(myConfigPath, true))
}

// instance App
const app = new App()

// add middleware and router

// read html
// app.use(async (req, res) => {

//     const htmlTemplatePath = path.resolve(__dirname, './index.html')
//     const htmlTemplate = readFile(htmlTemplatePath)
//     res.body = htmlTemplate
// })
app.use(router)


// start server
app.listen(myConfig.port || config.port, myConfig.host || config.host, () => {
    console.log('>>> server is start, http://' + config.host + ':' + config.port)
})